﻿// See https://aka.ms/new-console-template for more information
using Azure;
using Azure.AI.Inference;
using Microsoft.Extensions.AI;
using OpenAI.Chat;
using System.ComponentModel;
using System.Text;
using ChatMessage = Microsoft.Extensions.AI.ChatMessage;
using ChatRole = Microsoft.Extensions.AI.ChatRole;

Console.WriteLine("Hello, World!");
Uri uri = new Uri("https://models.github.ai/inference");

AzureKeyCredential azureKeyCredential = new AzureKeyCredential("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");

IChatClient chatClient = new ChatCompletionsClient(uri, azureKeyCredential).AsIChatClient("openai/gpt-4.1").AsBuilder().UseFunctionInvocation().Build();

//Uri uri = new Uri("http://localhost:11434");
//IChatClient chatClient = new OllamaChatClient(uri,"qwen2:7b").AsBuilder().UseFunctionInvocation().Build();


ChatOptions chatOptions = new ChatOptions()
{
    Tools = [AIFunctionFactory.Create(GetFood),AIFunctionFactory.Create(GetWeather)]
};

List<ChatMessage> messages = new List<ChatMessage>();

while (true)
{
    Console.Write("开始提问：");
    string msg=Console.ReadLine();
    //创建对话客户端
    messages.Add(new ChatMessage(ChatRole.User, msg));
    //通过对话客户端将消息发送给大模型
    var responses = chatClient.GetStreamingResponseAsync(messages, chatOptions);
    StringBuilder stringBuilder = new StringBuilder();
    Console.Write("人工智能回答：");
    await foreach (var response in responses)
    {
        Console.Write(response.Text);
        stringBuilder.Append(response.Text);
    }
    Console.WriteLine();
    messages.Add(new ChatMessage(ChatRole.Assistant, stringBuilder.ToString()));
}

//定义一个自定义服务
[Description("返回午餐信息")]
static string GetFood()
{
    return "吃鸡腿";
}

[Description("返回天气信息")]
static string GetWeather()
{
    return "今天大太阳";
}